<?php
class My_API_ss13_Users
{
    private $_config;
    private $_config2;
    private $_gameId;

    public function __construct($gameId = null)
    {
        if(!isset($gameId) || !file_exists(CONFIG_PATH2 . "/$gameId.ini")){
            return false;
        }
        $this->_gameId;
        
        $config = new Zend_Config_Ini(CONFIG_PATH2 . "/$gameId.ini",APPLICATION_ENV);
        $this->_config = $config;
        
        $config2 = new Zend_Config_Ini(CONFIG_PATH . "/$gameId.ini",APPLICATION_ENV);
        $this->_config2 = $config2;
    }

    public function getUsersByServerId($listUser,$serverId, $channel = '')
    {
		$i = 0;
		$infoUsers = array();
        if(strtolower($channel) != 'ssgroup'){
            $channel = '@'.strtolower($channel);
        }else{
            $channel = '';
        }
        $serverIdPartner = (isset($this->_config2->serverid->$serverId)) ? $this->_config2->serverid->$serverId : $serverId;
        
		$userCheck = array_chunk($listUser, 100);
		foreach ($userCheck as $listCheck){		
            $gameid = 'ssg';
            foreach($listCheck as $row){
                $users[] = $row.$channel;
            }
            $users = implode(",", $users);
            
            $urlAPI = $this->_config->getUserInfo->params->url."?user=$users&server_id=$serverIdPartner";
            
            $result = file_get_contents($urlAPI);
            $result = json_decode($result);
            
            foreach($result->result as $userName => $value){
				if(isset($value)){
                    $infoUsers[$i]['serverId'] = $serverId;
					$infoUsers[$i]['roleName'] = $value->roleName;
					$infoUsers[$i]['roleLevel'] = $value->level;
					$i++;
				}
			}
		}
        
		return $infoUsers;
    }
    
    public function getUsernameByRolename($listRoleName, $serverId){
        $i = 0;
		$infoUsername = array();
        $serverIdPartner = (isset($this->_config2->serverid->$serverId)) ? $this->_config2->serverid->$serverId : $serverId;
        
		$userCheck = array_chunk($listRoleName, 50);
		foreach ($userCheck as $listCheck){
            $accounts = implode(',',$listCheck);
            $urlAPI = $this->_config->getUsernameInfo->url . "?user=$accounts&server_id={$serverIdPartner}";
            $result = file_get_contents($urlAPI);
            $result = json_decode($result);
            
            if(isset($result->status) && $result->status == 1){
                foreach($result->result as $row){
					$infoUsername[$i]['serverId'] = $serverId;
					$infoUsername[$i]['username'] = $row->username;
					$infoUsername[$i]['roleLevel'] = $row->level;
					$i++;
                }
			}else{
				continue;
			}
		}
		return $infoUsername;
    }
}